var TableDatatablesAjax = function() {
  var datatableAjax = function(){
    dt = $('#datatable_ajax');
    ajax_datatable = dt.DataTable({
      "processing": true,
      "serverSide": true,
      "searching" : false,
     
      "ajax": {
        'url' : '/admin/wincome/ajaxIndex',
        "data": function ( d ) {
          console.log(d);
          d.project = $('.filter input[name="project"]').val();
          d.service = $('.filter input[name="service"]').val();
          d.amount = $('.filter input[name="amount"]').val();
          d.ticheng = $('.filter input[name="ticheng"]').val();
          d.activity = $('.filter input[name="activity"]').val();
          d.other = $('.filter input[name="other"]').val();
          d.time = $('.filter input[name="time"]').val();
          d.bar = $('.filter input[name="bar"]').val();
          d.created_at = $('.filter input[name="created_at"]').val();
          d.wincomebutton = $('.filter input[name="wincomebutton"]').val();
        }
      },
      "pagingType": "bootstrap_full_number",
      "order" : [],
      "orderCellsTop": true,
      "dom" : "<'row'<'col-sm-3'l><'col-sm-6'<'customtoolbar'>><'col-sm-3'f>>" +"<'row'<'col-sm-12'tr>>" +"<'row'<'col-sm-5'i><'col-sm-7'p>>",
      "columns": [
        {
          "data": "id",
          "name" : "id",
        },
        {
          "data": "project",
          "name" : "project",
          "orderable" : false,
          render:function(data){
            if (data == 1) {
              return '<span class="label label-success">上海亿大 </span>';
            }else if(data == 2){
              return '<span class="label label-warning">  昆明亿大 </span>';
            }else if(data == 3){
              return '<span class="label label-warning">  长治亿大 </span>';
            }else {
              return '<span class="label label-warning">  医生集团 </span>';
            }
          }
          
        },
        {
          "data": "service",
          "name": "service",
          "orderable" : false,
        },
        { 
          "data": "amount",
          "name": "amount",
          "orderable" : false,
         
        },
        { 
          "data": "ticheng",
          "name": "ticheng",
          "orderable" : true,
          
        },
        { 
          "data": "activity",
          "name": "activity",
          "orderable" : true,
        },
        { 
          "data": "other",
          "name": "other",
          "orderable" : true,
        },
          { 
          "data": "time",
          "name": "time",
          "orderable" : true,
        },
          { 
          "data": "created_at",
          "name": "created_at",
          "orderable" : true,
        },
          { 
          "data": "bar",
          "name": "bar",
          "orderable" : true,
           render:function(data){
            if (data == 1) {
              return '<span class="label label-success">渠道巴 </span>';
            }else{
              return '<span class="label label-success">技术巴 </span>'; 
            }
            
          }
        },
         {
          "data": "wincomebutton",
           "name": "wincomebutton",
           "type": "html",
           "orderable": false,
         },
        
      ],
      "drawCallback": function( settings ) {
        ajax_datatable.$('.tooltips').tooltip( {
          placement : 'top',
          html : true
        });  
      },
      "language": {
        url: '/admin/i18n'
      }
    });

    dt.on('click', '.filter-submit', function(){
      ajax_datatable.ajax.reload(); 
    });

    dt.on('click', '.filter-cancel', function(){
      $('textarea.form-filter, select.form-filter, input.form-filter', dt).each(function() {
          $(this).val("");
      });

      $('select.form-filter').selectpicker('refresh');

      $('input.form-filter[type="checkbox"]', dt).each(function() {
          $(this).attr("checked", false);
      });
      ajax_datatable.ajax.reload();
    });

    $('.input-group.date').datepicker({
      autoclose: true
    });
    $(".bs-select").selectpicker({
      iconBase: "fa",
      tickIcon: "fa-check"
    });
  };

  return {
    init : datatableAjax
  }
}();